// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.objects;

import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.SpriteDetails.IsoFlagType;
import zombie.iso.SpriteDetails.IsoObjectType;
import zombie.network.GameServer;

public class IsoWindowFrame {
    private static IsoWindowFrame.Direction getDirection(IsoObject isoObject) {
        if (!(isoObject instanceof IsoWindow) && !(isoObject instanceof IsoThumpable)) {
            if (isoObject == null || isoObject.getProperties() == null || isoObject.getObjectIndex() == -1) {
                return IsoWindowFrame.Direction.INVALID;
            } else if (isoObject.getProperties().Is(IsoFlagType.WindowN)) {
                return IsoWindowFrame.Direction.NORTH;
            } else {
                return isoObject.getProperties().Is(IsoFlagType.WindowW) ? IsoWindowFrame.Direction.WEST : IsoWindowFrame.Direction.INVALID;
            }
        } else {
            return IsoWindowFrame.Direction.INVALID;
        }
    }

    public static boolean isWindowFrame(IsoObject o) {
        return getDirection(o).isValid();
    }

    public static boolean isWindowFrame(IsoObject o, boolean north) {
        IsoWindowFrame.Direction direction = getDirection(o);
        return north && direction == IsoWindowFrame.Direction.NORTH || !north && direction == IsoWindowFrame.Direction.WEST;
    }

    public static int countAddSheetRope(IsoObject o) {
        IsoWindowFrame.Direction direction = getDirection(o);
        return direction.isValid() ? IsoWindow.countAddSheetRope(o.getSquare(), direction == IsoWindowFrame.Direction.NORTH) : 0;
    }

    public static boolean canAddSheetRope(IsoObject o) {
        IsoWindowFrame.Direction direction = getDirection(o);
        return direction.isValid() && IsoWindow.canAddSheetRope(o.getSquare(), direction == IsoWindowFrame.Direction.NORTH);
    }

    public static boolean haveSheetRope(IsoObject o) {
        IsoWindowFrame.Direction direction = getDirection(o);
        return direction.isValid() && IsoWindow.isTopOfSheetRopeHere(o.getSquare(), direction == IsoWindowFrame.Direction.NORTH);
    }

    public static boolean addSheetRope(IsoObject o, IsoPlayer player, String itemType) {
        return !canAddSheetRope(o) ? false : IsoWindow.addSheetRope(player, o.getSquare(), getDirection(o) == IsoWindowFrame.Direction.NORTH, itemType);
    }

    public static boolean removeSheetRope(IsoObject o, IsoPlayer player) {
        return !haveSheetRope(o) ? false : IsoWindow.removeSheetRope(player, o.getSquare(), getDirection(o) == IsoWindowFrame.Direction.NORTH);
    }

    public static IsoGridSquare getOppositeSquare(IsoObject o) {
        IsoWindowFrame.Direction direction = getDirection(o);
        if (!direction.isValid()) {
            return null;
        } else {
            boolean _boolean = direction == IsoWindowFrame.Direction.NORTH;
            return o.getSquare().getAdjacentSquare(_boolean ? IsoDirections.N : IsoDirections.W);
        }
    }

    public static IsoGridSquare getIndoorSquare(IsoObject o) {
        IsoWindowFrame.Direction direction = getDirection(o);
        if (!direction.isValid()) {
            return null;
        } else {
            IsoGridSquare isoGridSquare0 = o.getSquare();
            if (isoGridSquare0.getRoom() != null) {
                return isoGridSquare0;
            } else {
                IsoGridSquare isoGridSquare1 = getOppositeSquare(o);
                return isoGridSquare1 != null && isoGridSquare1.getRoom() != null ? isoGridSquare1 : null;
            }
        }
    }

    public static IsoCurtain getCurtain(IsoObject o) {
        IsoWindowFrame.Direction direction = getDirection(o);
        if (!direction.isValid()) {
            return null;
        } else {
            boolean _boolean = direction == IsoWindowFrame.Direction.NORTH;
            IsoCurtain isoCurtain = o.getSquare().getCurtain(_boolean ? IsoObjectType.curtainN : IsoObjectType.curtainW);
            if (isoCurtain != null) {
                return isoCurtain;
            } else {
                IsoGridSquare isoGridSquare = getOppositeSquare(o);
                return isoGridSquare == null ? null : isoGridSquare.getCurtain(_boolean ? IsoObjectType.curtainS : IsoObjectType.curtainE);
            }
        }
    }

    public static IsoGridSquare getAddSheetSquare(IsoObject o, IsoGameCharacter chr) {
        IsoWindowFrame.Direction direction = getDirection(o);
        if (!direction.isValid()) {
            return null;
        } else {
            boolean _boolean = direction == IsoWindowFrame.Direction.NORTH;
            if (chr != null && chr.getCurrentSquare() != null) {
                IsoGridSquare isoGridSquare0 = chr.getCurrentSquare();
                IsoGridSquare isoGridSquare1 = o.getSquare();
                if (_boolean) {
                    if (isoGridSquare0.getY() < isoGridSquare1.getY()) {
                        return isoGridSquare1.getAdjacentSquare(IsoDirections.N);
                    }
                } else if (isoGridSquare0.getX() < isoGridSquare1.getX()) {
                    return isoGridSquare1.getAdjacentSquare(IsoDirections.W);
                }

                return isoGridSquare1;
            } else {
                return null;
            }
        }
    }

    public static void addSheet(IsoObject o, IsoGameCharacter chr) {
        IsoWindowFrame.Direction direction = getDirection(o);
        if (direction.isValid()) {
            boolean _boolean = direction == IsoWindowFrame.Direction.NORTH;
            IsoGridSquare isoGridSquare = getIndoorSquare(o);
            if (isoGridSquare == null) {
                isoGridSquare = o.getSquare();
            }

            if (chr != null) {
                isoGridSquare = getAddSheetSquare(o, chr);
            }

            if (isoGridSquare != null) {
                IsoObjectType isoObjectType;
                if (isoGridSquare == o.getSquare()) {
                    isoObjectType = _boolean ? IsoObjectType.curtainN : IsoObjectType.curtainW;
                } else {
                    isoObjectType = _boolean ? IsoObjectType.curtainS : IsoObjectType.curtainE;
                }

                if (isoGridSquare.getCurtain(isoObjectType) == null) {
                    int _int = 16;
                    if (isoObjectType == IsoObjectType.curtainE) {
                        _int++;
                    }

                    if (isoObjectType == IsoObjectType.curtainS) {
                        _int += 3;
                    }

                    if (isoObjectType == IsoObjectType.curtainN) {
                        _int += 2;
                    }

                    _int += 4;
                    IsoCurtain isoCurtain = new IsoCurtain(o.getCell(), isoGridSquare, "fixtures_windows_curtains_01_" + _int, _boolean);
                    isoGridSquare.AddSpecialTileObject(isoCurtain);
                    if (GameServer.bServer) {
                        isoCurtain.transmitCompleteItemToClients();
                        if (chr != null) {
                            chr.sendObjectChange("removeOneOf", new Object[]{"type", "Sheet"});
                        }
                    } else if (chr != null) {
                        chr.getInventory().RemoveOneOf("Sheet");
                    }
                }
            }
        }
    }

    public static boolean canClimbThrough(IsoObject o, IsoGameCharacter chr) {
        IsoWindowFrame.Direction direction = getDirection(o);
        if (!direction.isValid()) {
            return false;
        } else if (o.getSquare() == null) {
            return false;
        } else {
            IsoWindow isoWindow = o.getSquare().getWindow(direction == IsoWindowFrame.Direction.NORTH);
            if (isoWindow != null && isoWindow.isBarricaded()) {
                return false;
            } else {
                if (chr != null) {
                    IsoGridSquare isoGridSquare = direction == IsoWindowFrame.Direction.NORTH
                        ? o.getSquare().nav[IsoDirections.N.index()]
                        : o.getSquare().nav[IsoDirections.W.index()];
                    if (!IsoWindow.canClimbThroughHelper(chr, o.getSquare(), isoGridSquare, direction == IsoWindowFrame.Direction.NORTH)) {
                        return false;
                    }
                }

                return true;
            }
        }
    }

    private static enum Direction {
        INVALID,
        NORTH,
        WEST;

        public boolean isValid() {
            return this != INVALID;
        }
    }
}
